/**
 * 
 */
package com.yidu.erp.action.department;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yidu.common.ExcelUtil;
import com.yidu.common.Pages;
import com.yidu.common.SsmMessage;
import com.yidu.erp.model.BeyondDepartment;
import com.yidu.erp.service.department.DepartmentService;

/**
 * 部门Action
 * @author zengren
 * @date 2017年11月24日
 * @version 1.0
 */
@Controller
@RequestMapping("department")
public class DepartmentAction {
	
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 显示列表
	 * @param param 查询及分页参数
	 * @return
	 */
	@RequestMapping("showList")
	@ResponseBody
	public Map<String, Object> showList(@RequestParam Map<String, Object> param, int limit, int page){
		if(limit!=0 && page!=0){
			Pages pages = new Pages();
			pages.setMaxResult(limit);
			pages.setCurPage(page);
			param.put("pages", pages);
		}
		return departmentService.showList(param);
	}
	
	/**
	 * 删除  
	 * @param departId 部门id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public SsmMessage delete(String departId){
		
		SsmMessage mes = new SsmMessage();
		
		int rows = departmentService.delete(departId);
		
		if(rows>0){
			mes.setMes(SsmMessage.SUCCESS_MES);
			mes.setState(SsmMessage.SUCCESS);
		}else{
			mes.setMes(SsmMessage.ERROR_MES);
			mes.setState(SsmMessage.ERROR);
		}
		
		return mes;
	}
	
	/**
	 * 增加或者修改
	 * @param department 部门对象，当对象中的id为空就增加，否则修改
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public SsmMessage addOrUpdate(BeyondDepartment department){
		
		SsmMessage mes = new SsmMessage();
		
		int rows = departmentService.addOrUpdate(department);
		
		if(rows>0){
			mes.setMes(SsmMessage.SUCCESS_MES);
			mes.setState(SsmMessage.SUCCESS);
		}else{
			mes.setMes(SsmMessage.ERROR_MES);
			mes.setState(SsmMessage.ERROR);
		}
		
		return mes;
	}
	
	/**
	 * 根据id查询
	 * @param departId 部门id
	 * @return
	 */
	@RequestMapping("findById")
	@ResponseBody
	public BeyondDepartment findById(String departId){
		return departmentService.findById(departId);
	}
	
	/**
	 * 导出Excel
	 * @param paramMap
	 * @param response
	 */
	@RequestMapping("educeExcel")
	public void educeExcel(String jsonStr, HttpServletResponse response){
		JSONObject json = JSONObject.parseObject(jsonStr);  
		JSONArray jsonArray = JSONArray.parseArray(json.getString("data"));
		Map<String, String> headMap = new HashMap<String, String>();
		headMap.put("departNumber", "部门编码");
		headMap.put("departName", "部门名称");
		headMap.put("departKind", "部门性质");
		headMap.put("departRemark", "备注");
		ExcelUtil.downloadExcelFile("部门列表", headMap, jsonArray, response);
	}
	
}
